package com.yuke.cloud.service.wmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillSkuDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.api.pmc.dto.RegionInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.wmc.dto.StorageDto;
import com.yuke.cloud.service.api.wmc.dto.StorageSpecInfoDto;
import com.yuke.cloud.service.api.wmc.dto.WareProductDto;
import com.yuke.cloud.service.wmc.dao.WmsStorageMapper;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import com.yuke.cloud.service.wmc.service.WmsStorageSpecService;
import com.yuke.cloud.service.wmc.service.WmsWarehouseShopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class WmsStorageSpecServiceImpl extends BaseService<WmsStorage> implements WmsStorageSpecService {
    @Resource
    private WmsStorageMapper wmsStorageMapper;
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsWarehouseShopService wmsWarehouseShopService;
    @Resource
    private PmcFeignApi pmcFeignApi;

    @Override
    public List<StorageSpecInfoDto> getAppStorageSpecByCondition(StorageSpecInfoDto storageSpecInfoDto, Long shopId) {
        List<StorageSpecInfoDto> storageSpecInfoDtos  = wmsStorageMapper.getAppStorageSpecByCondition(storageSpecInfoDto);
        return storageSpecInfoDtos;


    }

    @Override
    public List<StorageSpecInfoDto> getSpecInfoExtra(Long shopId, List<StorageSpecInfoDto> storageSpecInfoDtos, Byte saleType, Long warehouseId) {
        // 根据店铺获取所在城市的商品库存量
        List<Long> specIds = new ArrayList<>();
        List<Long> remoteSpecIds = new ArrayList<>();  // 需要从远程仓库获取信息的商品
        storageSpecInfoDtos.forEach(storageSpecInfoDto -> {
            specIds.add(storageSpecInfoDto.getSpecId());
            remoteSpecIds.add(storageSpecInfoDto.getSpecId());
        });

        if(shopId==null  ){
            shopId=220854931912392704L;
        }

        List<StorageSpecInfoDto> ssdto = new ArrayList<>();
        if (shopId != null) {
            // 获取店名
            List<Long> listShopIds = new ArrayList<>();
            listShopIds.add(shopId);


            if (specIds.size() > 0) {
                List<ProdSpecInfoDto> prodSpecInfoDtoList = pmcFeignApi.getSpecDtosBySpecIds(specIds);
                if (prodSpecInfoDtoList!=null && prodSpecInfoDtoList.size()>0){

                    RegionInfoDto regionInfoDto = pmcFeignApi.getProvinceAndCityInfoByCityId(storageSpecInfoDtos.get(0).getWareCityId());

                    for (StorageSpecInfoDto storageSpecInfoDto : storageSpecInfoDtos){

                            for (ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtoList){
                            //todo 20191114 如果有秒杀商品，把秒杀商品的价格统一为商家端销售价
                            if (null != storageSpecInfoDto.getIsSecKill()) {
                                if (storageSpecInfoDto.getIsSecKill()) {
                                    storageSpecInfoDto.setSecKillPrice(storageSpecInfoDto.getSalePrice());
                                }
                            }

                            if (storageSpecInfoDto.getStockCount() == null || storageSpecInfoDto.getStockCount() <= 0) {
                                storageSpecInfoDto.setStockCount(0) ;
                            } else {
                                remoteSpecIds.remove(Long.valueOf(storageSpecInfoDto.getSpecId()));  // 移去有库存量的商品
                            }
                            if (regionInfoDto != null) {

                                storageSpecInfoDto.setWareCityName(regionInfoDto.getRegionName());
                            }


                            if (storageSpecInfoDto.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                                storageSpecInfoDto.setSpecName(prodSpecInfoDto.getSpecName());
                                storageSpecInfoDto.setCategoryId(prodSpecInfoDto.getCategoryId());
                                storageSpecInfoDto.setBrandId(prodSpecInfoDto.getBrandId());
                                storageSpecInfoDto.setWeight(prodSpecInfoDto.getWeight());
                                storageSpecInfoDto.setBigPicture(prodSpecInfoDto.getBigPic());
                                storageSpecInfoDto.setBigPicUri(prodSpecInfoDto.getBigPic());
                                storageSpecInfoDto.setPicture(prodSpecInfoDto.getPicture());
                                storageSpecInfoDto.setDescription(prodSpecInfoDto.getDescription());
                                storageSpecInfoDto.setIntroduce(prodSpecInfoDto.getIntroduce());
                                storageSpecInfoDto.setSpecCode(prodSpecInfoDto.getSpecCode());
                                storageSpecInfoDto.setSupplierName(prodSpecInfoDto.getSupplierName());
                                if (storageSpecInfoDto.getNumber() - storageSpecInfoDto.getLockNumber() > 0){
                                    storageSpecInfoDto.setStockCount(storageSpecInfoDto.getNumber() - storageSpecInfoDto.getLockNumber());
                                }else{
                                    storageSpecInfoDto.setStockCount(0);
                                }

                                //todo 去掉生产日期
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                if (sdf.format(storageSpecInfoDto.getMfgDate()).equals("2020-04-18")){
                                    storageSpecInfoDto.setMfgDate(null);
                                }
                                //
                                ssdto.add(storageSpecInfoDto);

                            }
                        }
                    }

                }
            }

        }


        if (saleType != null && !saleType.equals((byte) 2)) {  // 店铺进货商品不获取秒杀价
          // add by wg 20190716 对远程仓库的货进行处理，如果前面获取到的货库存量为空或为0，则尝试从远程仓库获取
            if (shopId != null && remoteSpecIds.size() > 0 && warehouseId != null) {
                List<WareProductDto> listWareProductDto = wmsStorageService.getRemoteStockCount(remoteSpecIds, warehouseId);
                if (listWareProductDto != null && listWareProductDto.size()>0) {
                    for (StorageSpecInfoDto storageSpecInfoDto : ssdto) {
                        for (WareProductDto wareProductDto : listWareProductDto) {
                            if (wareProductDto.getSpecId().longValue() == storageSpecInfoDto.getSpecId().longValue()
                                    && wareProductDto.getStockCount() > 0) {
                                storageSpecInfoDto.setWarehouseId(wareProductDto.getWarehouseId());
                                storageSpecInfoDto.setStockCount(wareProductDto.getStockCount());
                                storageSpecInfoDto.setWareCityId(wareProductDto.getCityId());
                                storageSpecInfoDto.setWareCityName(wareProductDto.getProvinceName() + wareProductDto.getCityName());

                            }
                        }
                    }
                }
            }
            // add by wg 20190605 判断秒杀商品
            List<CmsSecKillSkuDto> secKillSkuInfoDtos = wmsStorageService.getSecKillSkus();


            for (StorageSpecInfoDto storageSpecInfoDto : ssdto) {
                for (CmsSecKillSkuDto secKillSkuInfoDto : secKillSkuInfoDtos) {
                    if (secKillSkuInfoDto.getSpecId().longValue() == storageSpecInfoDto.getSpecId().longValue()) {
                        if ((secKillSkuInfoDto.getStoreCount() - secKillSkuInfoDto.getLockCount()) > 0) {
                            storageSpecInfoDto.setIsSecKill(true);
                            //todo 20191104  SecKillPrice 改为salePrice。//secKillSkuInfoDto.getCmsSecKillSkuDto().getSecKillPrice()
                            storageSpecInfoDto.setSecKillPrice(storageSpecInfoDto.getSalePrice());
                            storageSpecInfoDto.setSecKillEndTime(secKillSkuInfoDto.getSecKillEndTime());
                        }
                    }
                }
            }
        }
        return ssdto;
    }

    @Override
    public List<StorageSpecInfoDto> getListRecommend(){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getListRecommend();
        storageSpecInfoDtos = getStorageSpecExtra(storageSpecInfoDtos);
        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getStorageInfoByCategoryIds(List<Long> categoryIds, Long warehouseId){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getStorageInfoByCategoryIds(categoryIds, warehouseId);
        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getBrandsByCategorys(List<Long> categoryIds,String specName,Byte saleType, Long warehouseId){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getBrandsByCategorys(categoryIds,specName,saleType, warehouseId);
        return storageSpecInfoDtos;
    }
    /**
     * *秒杀商品走的该接口，把小图换成了大图标
     * @param specIds
     * @param warehouseId
     * @return
     */
    @Override
    public List<StorageSpecInfoDto> getStorageInfoBySpecIds(List<Long> specIds, Long warehouseId){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getStorageInfoBySpecIds(specIds, warehouseId);

        storageSpecInfoDtos = getStorageSpecExtra(storageSpecInfoDtos);

        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getStorageSpecExtra(List<StorageSpecInfoDto> storageSpecInfoDtos){
        if (storageSpecInfoDtos != null && storageSpecInfoDtos.size()>0){
            List<Long> specIds = storageSpecInfoDtos.stream().map(StorageSpecInfoDto::getSpecId).collect(Collectors.toList());

            List<ProdSpecInfoDto> prodSpecInfoDtoList = pmcFeignApi.getSpecDtosBySpecIds(specIds);

            for(StorageSpecInfoDto storageSpecInfoDto : storageSpecInfoDtos){
                if (prodSpecInfoDtoList != null && prodSpecInfoDtoList.size()>0){
                    for (ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtoList){
                        if (storageSpecInfoDto.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                            storageSpecInfoDto.setProductName(prodSpecInfoDto.getProductName());
                            storageSpecInfoDto.setWeight(prodSpecInfoDto.getWeight());
                            storageSpecInfoDto.setPicture(prodSpecInfoDto.getPicture());
                            storageSpecInfoDto.setBigPicture(prodSpecInfoDto.getBigPic());
                            storageSpecInfoDto.setDescription(prodSpecInfoDto.getDescription());
                            storageSpecInfoDto.setIntroduce(prodSpecInfoDto.getIntroduce());
                            storageSpecInfoDto.setSpecCode(prodSpecInfoDto.getSpecCode());
                            storageSpecInfoDto.setSalePrice(storageSpecInfoDto.getMarketPrice());
                            if (storageSpecInfoDto.getNumber()-storageSpecInfoDto.getLockNumber() > 0){
                                storageSpecInfoDto.setStockCount(storageSpecInfoDto.getNumber()-storageSpecInfoDto.getLockNumber());
                            }else{
                                storageSpecInfoDto.setStockCount(0);
                            }

                        }
                    }
                }

            }


        }else{
            return null;
        }

        return storageSpecInfoDtos;
    }


    @Override
    public List<ProdSpecInfoExDto> getStorageSpecBySpecIds(Long warehouseId, List<Long> specIds, Byte cartType){
        List<ProdSpecInfoExDto> prodSpecInfoExDtoList =  wmsStorageMapper.getStorageSpecBySpecIds(cartType, warehouseId, specIds );
        prodSpecInfoExDtoList = getProdSpecExtra(prodSpecInfoExDtoList);
        return prodSpecInfoExDtoList;
    }


    private List<ProdSpecInfoExDto> getProdSpecExtra(List<ProdSpecInfoExDto> prodSpecInfoExDtoList){
        if (prodSpecInfoExDtoList != null && prodSpecInfoExDtoList.size()>0){
            List<Long> specIds = prodSpecInfoExDtoList.stream().map(ProdSpecInfoExDto::getSpecId).collect(Collectors.toList());

            List<ProdSpecInfoExDto> list = pmcFeignApi.getAppSpecInfoBySpecIds(specIds);

            for(ProdSpecInfoExDto prodSpecInfoExDto : prodSpecInfoExDtoList){
                if (list != null && list.size()>0){
                    for (ProdSpecInfoExDto item : list){
                        if (prodSpecInfoExDto.getSpecId().equals(item.getSpecId())){
                            prodSpecInfoExDto.setWeight(item.getWeight());
                            prodSpecInfoExDto.setPicture(item.getBigPicture());
                            prodSpecInfoExDto.setDescription(item.getDescription());
                            prodSpecInfoExDto.setSecKillPrice(prodSpecInfoExDto.getSalePrice());
                            prodSpecInfoExDto.setIsSecKill(item.getIsSecKill());
                            prodSpecInfoExDto.setSecKillEndTime(item.getSecKillEndTime());
                            if (prodSpecInfoExDto.getNumber() - prodSpecInfoExDto.getLockNumber() < 0){
                                prodSpecInfoExDto.setStockCount(0);
                            }else{
                                prodSpecInfoExDto.setStockCount(prodSpecInfoExDto.getNumber() - prodSpecInfoExDto.getLockNumber());
                            }
                        }
                    }
                }
            }

            // add by wg 20190605 判断秒杀商品
            List<CmsSecKillSkuDto> secKillSkuInfoDtos = wmsStorageService.getSecKillSkus();

            for (CmsSecKillSkuDto secKillSkuInfoDto : secKillSkuInfoDtos) {
                for (ProdSpecInfoExDto item : prodSpecInfoExDtoList) {
                    if (secKillSkuInfoDto.getSpecId().longValue() == item.getSpecId().longValue()) {
                        if ((secKillSkuInfoDto.getStoreCount() - secKillSkuInfoDto.getLockCount()) > 0) {
                            item.setIsSecKill(true);
                            item.setSecKillEndTime(secKillSkuInfoDto.getSecKillEndTime());
                            item.setSecKillPrice(item.getSalePrice());
                        }
                    }
                }
            }
        }else{
            return null;
        }
        return prodSpecInfoExDtoList;
    }


    //先获取商品信息，在从库存信息里面拿价格，仓库信息等值
    //适用于秒杀商品，活动商品的商品信息获取
    @Override
    public List<StorageSpecInfoDto> getProdStorageSpecBySpecIds(List<Long> specIds, Long shopId){
        List<StorageSpecInfoDto> ssidList = this.getStorageInfoBySpecIds(specIds,shopId);
        return ssidList;

    }

    @Override
    public List<StorageSpecInfoDto> getProdSpecInfo(List<Long> specIds) {
        List<StorageSpecInfoDto> storageSpecInfoDtos = wmsStorageMapper.getProdSpecInfo(specIds);
        storageSpecInfoDtos = getStorageSpecExtra(storageSpecInfoDtos);
        return storageSpecInfoDtos;
    }

    @Override
    public List<ProdSpecInfoExDto> getStorageSpecByStorageIds(List<Long> storageIds, Byte cartType){
        List<ProdSpecInfoExDto> ssidList = wmsStorageMapper.getStorageSpecByStorageIds(storageIds,cartType);
        ssidList = getProdSpecExtra(ssidList);
        return ssidList;

    }

    @Override
    public List<StorageSpecInfoDto> getAllStorageInfoBySpecIds(List<Long> specIds, Long warehouseId){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getAllStorageInfoBySpecIds(specIds, warehouseId);
        System.out.println("specIds请求的数据111=========="+ storageSpecInfoDtos);
        storageSpecInfoDtos = getStorageSpecExtra(storageSpecInfoDtos);
        System.out.println("specIds请求的数据222=========="+ storageSpecInfoDtos);
        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getAllStorageBySpecIds(List<Long> specIds, Long warehouseId){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getAllStorageBySpecIds(specIds, warehouseId);
        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getProdSpecGoods(StorageSpecInfoDto storageSpecInfoDto){
        List<StorageSpecInfoDto> storageSpecInfoDtos =  wmsStorageMapper.getProdSpecGoods(storageSpecInfoDto);
        storageSpecInfoDtos = getStorageSpecExtra(storageSpecInfoDtos);
        return storageSpecInfoDtos;
    }

    @Override
    public List<StorageSpecInfoDto> getWmsCategoryBrand(StorageSpecInfoDto prodCategoryBrandDto, Long shopId) {
        // add by wg 20190611 对排序方式进行判断

        if (prodCategoryBrandDto != null && prodCategoryBrandDto.getSortPriceType() != null) {
            String sortPriceType = prodCategoryBrandDto.getSortPriceType();
            if (!sortPriceType.equalsIgnoreCase("asc") && !sortPriceType.equalsIgnoreCase("desc")) {
                return new ArrayList<>();
            }
        }
        if (prodCategoryBrandDto != null && prodCategoryBrandDto.getSortCountType() != null) {
            String sorCountType = prodCategoryBrandDto.getSortCountType();
            if (!sorCountType.equalsIgnoreCase("asc") && !sorCountType.equalsIgnoreCase("desc")) {
                return new ArrayList<>();
            }
        }
        List<StorageSpecInfoDto> prodCategoryBrand = new ArrayList<>();

        if (prodCategoryBrandDto.getCategoryId() != null && prodCategoryBrandDto.getSpecName() != null){
            prodCategoryBrandDto.setSpecName("");
        }

        prodCategoryBrand = wmsStorageMapper.getWmsCategoryBrand(prodCategoryBrandDto);

        //获取商品库存以及商品所在城市Id以及城市名称和所属仓库  判断商品是否为秒杀价格
        //todo
        prodCategoryBrand = getStorageSpecExtra(prodCategoryBrand);
        return prodCategoryBrand;
    }

    @Override
    public List<StorageDto> selectWmsSpecsByProductId(StorageDto storageDto){
        return wmsStorageMapper.selectWmsSpecsByProductId(storageDto);
    }

    @Override
    public List<StorageSpecInfoDto> getProductSpecByCondition(StorageSpecInfoDto storageSpecInfoDto){
        return wmsStorageMapper.getProductSpecByCondition(storageSpecInfoDto);
    }
}
